package attori.controller;

import sde.actor.Actor;
import sde.actor.Message;
import attori.attuatori.AttuatoreLuceIF;

public class ControllerLuce extends Actor implements ControllerLuceIF {

	public static final byte CREATED = 0, OFF = 1, ON = 2;
	private int volume;
	private AttuatoreLuceIF luce;

	public ControllerLuce() {
		become(CREATED);
	}

	@Override
	protected void handler(Message m) {
		switch (currentStatus()) {
		case CREATED:
			if (m instanceof Init) {
				volume = ((Init) m).valore;
				luce = ((Init) m).au;
				become(OFF);
				System.out.println("sono off");
			}
			break;
		case OFF:
			if (m instanceof Volume && ((Volume) m).valore > volume) {
				luce.send(new AttuatoreLuceIF.ToOpen());
				become(ON);
			}
			break;
		case ON:
			if (m instanceof Volume && ((Volume) m).valore <= volume) {
				luce.send(new AttuatoreLuceIF.ToClose());
				become(OFF);
			}
			break;
		}
	}
}
